package com.mzj.saas.mservice.currency.service;

import com.mzj.saas.mservice.lock.Repository.SmartLockAuthRepository;
import com.mzj.saas.mservice.lock.Repository.SmartLockRepository;
import com.mzj.saas.mservice.lock.entity.SmartLock;
import com.mzj.saas.mservice.lock.entity.SmartLockAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 *  智能锁通用接口
 */
@Service
public class LockCurrencyService {
    private static final Logger log = LoggerFactory.getLogger(LockCurrencyService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SmartLockRepository smartLockRepository;
    @Autowired
    private SmartLockAuthRepository smartLockAuthRepository;


    /**
     * 验证智能锁，或者 密码， 指纹 身份证 CPU卡  蓝牙 是否冻结
     * true  代表未冻结
     * false  代表已冻结
     * @param lockId
     * @param lockAuthId
     * @return
     */
    public Boolean freezeOrNot(Long lockId,Long lockAuthId ) {

        Optional<SmartLock> optionalLock = smartLockRepository.findById(lockId);
        if (!optionalLock.isPresent()){
           return false;
        }
        SmartLock smartLock = optionalLock.get();
        //设备状态:0未激活 1正常 2冻结
        if (smartLock.getMechineStatus() == 2){
            return false;
        }

        if (lockAuthId != null ){
                String sql="select * from   saas_smart_lock_auth " +
                        "where id =? and (pwd_status=2 or fingerprint_status=2 or card_status=2 or cpu_status=2 or ble_status=2 or face_status=2)";
            List<SmartLockAuth> lockAuth = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SmartLockAuth.class), lockAuthId);
            if (lockAuth != null &&  lockAuth.size()>0){
                return false;
            }

        }

        return true;
    }

}
